/*
 * Copyright (c) 2006-2008, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.livejournal;

import java.awt.Color;

import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JList;

import org.panteleyev.ljapi.LJAccount;
import org.panteleyev.ljapi.LJFriend;
import org.panteleyev.ljapi.LJGroup;
import org.panteleyev.swing.BasicDialog;
import org.panteleyev.swing.CheckListCellRenderer;
import org.panteleyev.swing.OkCancelButtonPanel;
import org.panteleyev.swing.SimpleListModel;

public class FriendDialog extends BasicDialog<JFrame> {
    LJAccount _account;
    int groupMask;
    int bg;
    int fg;
    private SimpleListModel groupListModel;

    public FriendDialog(JFrame parent, LJFriend friend, LJAccount account, boolean editName) {
        super(parent);
        initComponents();
        initComponentsEx();
        createGroupList();

        _account = account;

        if (friend != null) {
            setTitle(L10N.FRIEND_DIALOG_TITLE_EDIT.s());
            this.fg = friend.getFg();
            this.bg = friend.getBg();
            this.groupMask = friend.getGroupMask();
            usernameEdit.setText(friend.getUserName());
            sampleLabel.setText(friend.getUserName());
        } else {
            setTitle(L10N.FRIEND_DIALOG_TITLE_ADD.s());
            this.fg = 0;
            this.bg = 0xFFFFFF;
            this.groupMask = 0;
            usernameEdit.setText("");
            sampleLabel.setText(" ");
        }

        getContentPane().add(new OkCancelButtonPanel(this));

        usernameEdit.setEnabled(editName);
        setSampleColors();
        pack();
    }

    private void initComponentsEx() {

    }

    private void createGroupList() {
        groupList.setCellRenderer(new CheckListCellRenderer() {
            @Override
            public java.awt.Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
                LJGroup group = _account.getGroup(index);
                if (group != null) {
                    setText(group.getName());
                    if ((groupMask & (1 << group.getNumber())) != 0) {
                        setSelected(true);
                    } else {
                        setSelected(false);
                    }
                }
                setBackground(isSelected ? list.getSelectionBackground() : Color.WHITE);
                setForeground(isSelected ? list.getSelectionForeground() : list.getForeground());
        //        setFont(table.getFont());
//                setBorder(hasFocus ? focusBorder : noFocusBorder);
                return this;
            }
        });

        groupListModel = new SimpleListModel() {
            @Override
            public Object getElementAt(int index) { return null; }
            @Override
            public int getSize() { return _account.getGroupsCount(); }
        };

        groupList.setModel(groupListModel);
    }

    public String getUserName() { return this.usernameEdit.getText(); }
    public int getGroupMask() { return this.groupMask; }
    public int getFg() { return this.fg; }
    public int getBg() { return this.bg; }

    private void setSampleColors() {
        sampleLabel.setForeground(new java.awt.Color(this.fg));
        samplePanel.setBackground(new java.awt.Color(this.bg));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usernamePanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        usernameEdit = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        groupList = new javax.swing.JList();
        colorPanel = new javax.swing.JPanel();
        colorButtonPanel = new javax.swing.JPanel();
        bgColorButton = new javax.swing.JButton();
        fgColorButton = new javax.swing.JButton();
        samplePanel = new javax.swing.JPanel();
        sampleLabel = new javax.swing.JLabel();

        setResizable(false);
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));

        jLabel1.setText("Friend Username:");
        usernamePanel.add(jLabel1);

        usernameEdit.setColumns(15);
        usernameEdit.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                usernameEditCaretUpdate(evt);
            }
        });
        usernamePanel.add(usernameEdit);

        getContentPane().add(usernamePanel);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(L10N.FRIEND_DIALOG_GROUPS.s()));
        jPanel1.setLayout(new java.awt.BorderLayout());

        groupList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        groupList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                groupListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(groupList);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1);

        colorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(L10N.FRIEND_DIALOG_COLORS.s()));
        colorPanel.setLayout(new javax.swing.BoxLayout(colorPanel, javax.swing.BoxLayout.Y_AXIS));

        bgColorButton.setText(L10N.FRIEND_DIALOG_BACKGROUND.s() + "...");
        bgColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bgColorButtonActionPerformed(evt);
            }
        });
        colorButtonPanel.add(bgColorButton);

        fgColorButton.setText(L10N.FRIEND_DIALOG_FOREGROUND.s() + "...");
        fgColorButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fgColorButtonActionPerformed(evt);
            }
        });
        colorButtonPanel.add(fgColorButton);

        colorPanel.add(colorButtonPanel);

        sampleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        sampleLabel.setText("jLabel2");
        samplePanel.add(sampleLabel);

        colorPanel.add(samplePanel);

        getContentPane().add(colorPanel);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void usernameEditCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_usernameEditCaretUpdate
        String text = usernameEdit.getText();
        sampleLabel.setText((text.length() == 0)? " " : text);
        pack();
    }//GEN-LAST:event_usernameEditCaretUpdate

    private void fgColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fgColorButtonActionPerformed
        Color c = JColorChooser.showDialog(getParent(), L10N.FRIEND_DIALOG_FOREGROUND.s(), new Color(this.fg));
        if (c != null) {
            this.fg = c.getRGB();
            setSampleColors();
        }
    }//GEN-LAST:event_fgColorButtonActionPerformed

    private void bgColorButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bgColorButtonActionPerformed
        Color c = JColorChooser.showDialog(getParent(), L10N.FRIEND_DIALOG_BACKGROUND.s(), new Color(this.bg));
        if (c != null) {
            this.bg = c.getRGB();
            setSampleColors();
        }
    }//GEN-LAST:event_bgColorButtonActionPerformed

    private void groupListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_groupListMouseClicked
        if (evt.getClickCount() == 2) {
            int index = groupList.getSelectedIndex();
            if (index != -1) {
                LJGroup group = _account.getGroup(index);
                if (group != null) {
                    groupMask ^= 1 << group.getNumber();
                    groupListModel.fireListDataChanged();
                }
            }
        }
    }//GEN-LAST:event_groupListMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bgColorButton;
    private javax.swing.JPanel colorButtonPanel;
    private javax.swing.JPanel colorPanel;
    private javax.swing.JButton fgColorButton;
    private javax.swing.JList groupList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel sampleLabel;
    private javax.swing.JPanel samplePanel;
    private javax.swing.JTextField usernameEdit;
    private javax.swing.JPanel usernamePanel;
    // End of variables declaration//GEN-END:variables

}
